<?php
class FrondendsController  extends AppController {

	//var $name = null;
	var $uses = array('User', 'Category','Job');
	function beforefilter(){
		$this->layout='default1';
		//$this->helpers['Paginator'] = array('ajax' => 'Ajax');
/*		$this->Paginator->options(array(
    	'update' => '#jobs',
    	'evalScripts' => true
		));
*/
		//$this->_setLanguage();
		//debug($this->passedArgs);
		//$this->helpers['Paginator'] = array('ajax' => 'Ajax');
	}
	function index() {
		$this->redirect('home');
	}
	function test(){
	}
	function search(){
		$options = array(
        	'limit' => 4,
		//'update' => '#jobs',
		//'evalScripts' => true
		);
		$this->paginate = $options;
		if(isset($_GET['query'])){
			$cond = array('Job.name like' => '%'.$_GET['query'].'%');
			//$this->set('jobs',$this->paginate("Job", $cond));
			$this->set('jobs',$this->paginate('Job',$cond));
			$this->set('query',$_GET['query']);
			//debug($this->paginate('Job',$cond));
			if(!$this->Session->check('User')){
				//debug('aaaa'.$this->Session->check('User'));
				$this->set('isadmin',false);
			}else{
				$this->set('isadmin',true);
			}
		}
	}
	function searchAjax(){
		$options = array(
        	'limit' => 4,
		);
		$this->paginate = $options;
		if(isset($_GET['query'])){
			//debug($_GET['query']);
			$cond = array('Job.name like' => '%'.$_GET['query'].'%');
			//$this->set('jobs',$this->paginate("Job", $cond));
			$this->set('jobs',$this->paginate('Job',$cond));
			$this->set('query',$_GET['query']);
			//debug($this->paginate('Job',$cond));
			if(!$this->Session->check('User')){
				//debug('aaaa'.$this->Session->check('User'));
				$this->set('isadmin',false);
			}else{
				$this->set('isadmin',true);
			}
			//echo json_encode($this->paginate('Job',$cond)); die;
		}
		$this->viewPath='frondends';
		$this -> render('search');
	}

	function add(){
		$this->pageTitle = 'Your best job board';
		$this->set('title_for_layout','Your best job board');
		$catagories=$this->Category->find('list');
		$this->set(compact('catagories',$catagories));
		$users = $this->Job->User->find('list');
		//debug(compact('categories', 'newVar'));
		$this->set(compact('users', 'users'));
		if (!empty($this->data)) {
			//debug($this->data);
			$this->Job->create();
			if ($this->Job->save($this->data)) {
				$this->Session->setFlash(__('The job has been saved', true));
				//$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('The job could not be saved. Please, try again.', true));
			}
			return;
		}
	}
	function home(){
		$params=array(
		'limit' => 2, //int
		'page' => 0, //int
		'offset'=>0, //int
		'callbacks' => true //other possible values are false, 'before', 'after'
		);
		$catagories=$this->Category->find('all');
		$this->set('categories',$catagories);
		//debug($catagories);
		//$cond = array('Category.name' => 'Design');
		//$this->set('designs',$this->paginate("Job", $cond));
		//debug($this->paginate("Job", $cond));
		//$cond = array('Category.name' => 'Programming');
		//$this->set('programmings',$this->paginate("Job", $cond));
		//$this->set('catagories',$catagories);
		//foreach ($catagories as $catagory){
		//debug('aaa'.$catagory->name);
		//$cond = array('Category.name' => $catagory['Category']['name']);
		//$this->set($catagory['Category']['name'],$this->paginate("Job", $cond));
		//debug($this->paginate("Job", $cond));
		//}
		/*
		 $categories = $this->Category->find('list');
		 //debug($categories);
		 $result = array();
		 foreach ($categories as $category) {
			$this->paginate = array(
			'conditions' => array('Job.category_id' => $category),
			'limit' => 3,
			'order' =>
			)
			}
			*/
	}
	function _setLanguage() {

		if ($this->Cookie->read('lang') && !$this->Session->check('Config.language')) {
			$this->Session->write('Config.language', $this->Cookie->read('lang'));
		}
		else if (isset($this->params['language']) && ($this->params['language']
		!=  $this->Session->read('Config.language'))) {

			$this->Session->write('Config.language', $this->params['language']);
			$this->Cookie->write('lang', $this->params['language'], false, '20 days');
		}
	}
}
?>